﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace RDPCommon.Server
{
    public class ServerTool
    {
        //static string m_RDPSystem = "RDPSystem";

        /// <summary>
        /// 服务端远程对象初始化
        /// </summary>
        /// <param name="objs"></param>
        /// <returns></returns>
        public static object[] ServerObjectInit( object[] objs)
        {
            object[] rtn = new object[] { 0, "", null };
            object[] _robjs = new object[] { "", "RDPSystem", "RDPSystem.ServerObject", "ServerObjectInit" };
            object[] _parms = new object[] { };

            object[] resp = null;
            try
            {
                resp = GetObjects(_robjs, _parms);
                if (resp != null && Convert.ToInt32(((object[])resp[0])[0]) != 0)
                {
                    rtn[0] = 1;
                    rtn[1] = ((object[])resp[0])[1];
                }
            }
            catch (Exception ex)
            {
                rtn[0] = 1;
                rtn[1] = ex;
            }
            return rtn;
        }

        /// <summary>
        /// 向客户端广播
        /// </summary>
        /// <param name="objs"></param>
        /// <returns></returns>
        public static object[] BroadCastingInfo(object[] objs)
        {
            object[] rtn = new object[] { 0, "", null };
            object[] clients = objs[0] as object[];
            string msg = objs[1].ToString();

            object[] _robjs = new object[] { "", "RDPSystem", "RDPSystem.ServerObject", "BroadCastingInfo" };
            object[] _parms = new object[] { clients, msg };

            object[] resp = null;
            try
            {
                resp = GetObjects(_robjs, _parms);
                if (resp != null && Convert.ToInt32(((object[])resp[0])[0]) != 0)
                {
                    rtn[0] = 1;
                    rtn[1] = ((object[])resp[0])[1];
                }
            }
            catch (Exception ex)
            {
                rtn[0] = 1;
                rtn[1] = ex;
            }
            return rtn;
        }

        /// <summary>
        /// 测试客户端是否在线
        /// </summary>
        /// <param name="objs"></param>
        /// <returns></returns>
        public static object[] OnlineTest(object[] objs)
        {
            object[] rtn = new object[] { 0, "", null };
            object[] _robjs = new object[] { "", "RDPSystem", "RDPSystem.ServerObject", "OnLineTest" };
            object[] _parms = new object[] { objs[0] };
            object[] resp = null;
            try
            {
                resp = GetObjects(_robjs, _parms);
                if (resp != null && Convert.ToInt32(((object[])resp[0])[0]) != 0)
                {
                    rtn[0] = 1;
                    rtn[1] = ((object[])resp[0])[1];
                }
            }
            catch (Exception ex)
            {
                rtn[0] = 1;
                rtn[1] = ex;
            }
            return rtn;
        }

        /// <summary>
        /// 帮助方法
        /// </summary>
        /// <param name="ocls"></param>
        /// <param name="opms"></param>
        /// <returns></returns>
        public static object[] GetObjects(object[] ocls, object[] opms)
        {
            string _app = ocls[0].ToString();
            if (_app != "")
                _app = "\\" + _app;
            string _pak = ocls[1].ToString();
            string _cls = ocls[2].ToString();
            string _mod = ocls[3].ToString();
            Assembly _a = Assembly.LoadFrom(AppDomain.CurrentDomain.BaseDirectory + _app + "\\" + _pak + ".dll");
            Type _t = _a.GetType(_cls);
            object _obj = Activator.CreateInstance(_t);
            MethodInfo _mi = _t.GetMethod(_mod);
            return new object[] { _mi.Invoke(_obj, new object[]{opms}) };
        }
    }
}
